package com.controller.front_controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mypg.User;
import com.mypg.UserRank;
import com.service.CollectService;
import com.service.UserQuestionService;
import com.service.UserService;
import org.apache.commons.fileupload.util.Streams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileOutputStream;
import java.util.*;

@RequestMapping("/front_user")
@Controller
public class FrontUserController {

    @Autowired
    UserService userService;

    @Autowired
    UserQuestionService userQuestionService;

    @Autowired
    CollectService collectService;

    @RequestMapping("/user_init_person.do")
    @ResponseBody
    public Map<String,Object> doUser_init_person(HttpSession session){
        Map<String,Object> map=new HashMap<>();
        try{
            String user_name= (String) session.getAttribute("user_name");
            int user_id=userService.selectUserByUser_name(user_name).getUser_id();
            String head_img=userService.selectUserByUser_name(user_name).getUser_img();
            int right_num=userQuestionService.selectCountByFlag_pass_and_user_id(user_id,1,"","");
            int wrong_num=userQuestionService.selectCountByFlag_pass_and_user_id(user_id,0,"","");
            int collect_num=collectService.selectCountByUser_id(user_id,"","");
            int did_num=right_num+wrong_num;
            map.put("head_img",head_img);
            map.put("did",did_num);
            map.put("collect",collect_num);
            map.put("wrong",wrong_num);
        }catch (Exception e){
            map.put("head_img","default_user_img.jpg");
            map.put("did",0);
            map.put("collect",0);
            map.put("wrong",0);
        }
        return map;
    }

    @RequestMapping("/change_user_img.do")
    @ResponseBody
    public String doChange_user_img(HttpSession session, @RequestParam(value = "file",required = false)MultipartFile file) throws JsonProcessingException {
        Map<String,Object> map=new HashMap<>();
        map.put("code",0);
        map.put("msg","");
        String user_name= (String) session.getAttribute("user_name");
        String img_name= UUID.randomUUID()+"_with_"+file.getOriginalFilename();
        String path= (String) session.getAttribute("img_save_path");
        path+="/user_img/";
        String img_raw=userService.selectUserByUser_name(user_name).getUser_img();
       if(!file.isEmpty()){
           if(!"default_user_img.jpg".equals(img_raw)) {
               File file1 = new File(path + img_raw);
               file1.delete();
           }
           try{
               File uploadFile=new File(path+img_name);
               Streams.copy(file.getInputStream(),new FileOutputStream(uploadFile.getAbsolutePath()),true);
           }catch (Exception e){
               img_name="default_user_img.jpg";
           }
           userService.updateUser_imgByUser_name(user_name,img_name);
       }
       map.put("data",img_name);
       return new ObjectMapper().writeValueAsString(map);
    }

    @RequestMapping("refresh_user_img.do")
    @ResponseBody
    public String doRefresh_user_img(HttpSession session){
        String user_name= (String) session.getAttribute("user_name");
        String user_img=userService.selectUserByUser_name(user_name).getUser_img();
        return user_img;
    }

    @RequestMapping("/init_user_rank.do")
    @ResponseBody
    public Map<String,Object> doInit_user_rank(HttpSession session){
        String user_name= (String) session.getAttribute("user_name");
        int user_id=userService.selectUserByUser_name(user_name).getUser_id();
        List<UserRank> rankList=userQuestionService.selectUserRank();
        List<User> userList=new ArrayList<>();
        int current_rank=0;
        int i=1;
        for(UserRank userRank:rankList){
            if(userRank.getUser_id()==user_id)
                current_rank=i;
            User user=userService.selectUserByUser_id(userRank.getUser_id());
            userList.add(user);
            i++;
        }
        Map<String,Object> map=new HashMap<>();
        map.put("rankList",rankList);
        map.put("userList",userList);
        map.put("current_rank",current_rank);
        return map;
    }
}
